﻿using Adnc.Application.Shared.AliyunAudit;
using Adnc.Shared.ConfigModels;
using Adnc.Shared.RpcServices.Services;
using Autofac;

namespace WYT.Sys.WebApi
{
    public class Startup
    {
        private readonly IHostEnvironment _environment;

        private IServiceCollection _services;

        private readonly IConfiguration _config;


        public Startup(IHostEnvironment environment, IConfiguration config)
        {
            _environment = environment;
            _config = config;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            _services = services;
            services.AddWYTServices(registion => {
                var policies = registion.GenerateDefaultRefitPolicies();
#if DEBUG
                registion.AddRpcService<IUserRpcService>("http://gateway.wyuntao.com", policies);
#else
                registion.AddRpcService<IUserRpcService>("wyt.usr.webapi", policies);
#endif
            });
            services.Configure<AliyunOptions>(_config.GetSection("AliyunOptions"));
        }

        public void ConfigureContainer(ContainerBuilder builder)
        {
            builder.RegisterAdncModules(_services);
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseAdncMiddlewares();

            if (_environment.IsProduction() || _environment.IsStaging())
            {
                app.RegisterToConsul();
            }
        }
    }
}
